package com.hw.one.collect.anyang.data;

import com.hw.one.collect.dao.FiveMinDataDao;
import com.hw.one.collect.param.SmallAirParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/17 14:50.
 */
@Service
public class AnYangPushService {

    @Resource
    private FiveMinDataDao fiveMinDataDao;

    /**
     * 根据设备id查询数据,并拼装数据
     * @param snCodeList
     * @return
     * @throws ParseException
     */
    public List<String> findFiveMinData(List<String> snCodeList)  {

        DecimalFormat df = new DecimalFormat("#0");
        DecimalFormat df1 = new DecimalFormat("#0.0");
        DecimalFormat df2 = new DecimalFormat("#0.00");
        DecimalFormat df3 = new DecimalFormat("#0.000");

//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("snCodeList", snCodeList);
        SmallAirParam smallAirParam = new SmallAirParam();
        smallAirParam.setSNCodeList(snCodeList);
        List<Map> resultList = fiveMinDataDao.findRaiseDustFiveMinDataBySNCodeList(smallAirParam);

        List<String> returnList = new ArrayList<>();
        for (Map minData : resultList) {
            // 设备编号
            Map<String, Object> monitorData = (Map<String, Object>) minData.get("MONITORDATA");
            String snCode = minData.get("SNCODE").toString();
            String monitorTime = minData.get("MONITORTIME").toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date monitorDate = null;
            try {
                monitorDate = sdf.parse(monitorTime);
            }catch (Exception e){
                e.printStackTrace();
                monitorDate = new Date();
            }
            // 监测时间时间戳
            long date = monitorDate.getTime();

            Double pm25 = monitorData.get("CTPM25") == null ? 0 : Double.valueOf(monitorData.get("CTPM25").toString());
            Double pm10 =  monitorData.get("CTPM10") == null ? 0 : Double.valueOf(monitorData.get("CTPM10").toString());
            Double temp =  monitorData.get("TEMP") == null ? 0 : Double.valueOf(monitorData.get("TEMP").toString());
            Double hum =  monitorData.get("HUM") == null ? 0 : Double.valueOf(monitorData.get("HUM").toString());
            Double noise =  monitorData.get("NOISE") == null ? 0 : Double.valueOf(monitorData.get("NOISE").toString());
            Double ws =  monitorData.get("WS") == null ? 0 : Double.valueOf(monitorData.get("WS").toString());
            Double wd =  monitorData.get("WD") == null ? 0 : Double.valueOf(monitorData.get("WD").toString());
            Double pressure =  monitorData.get("PRESSURE") == null ? 0 : Double.valueOf(monitorData.get("PRESSURE").toString());

            String pm25Str = df.format(pm25); // 整数
            String pm10Str = df.format(pm10); // 整数
            String humStr = df.format(hum); // 整数
            String wdStr = df.format(wd); // 整数

            String tempStr;
            if (temp == 0) {
                tempStr = "0";
            }else {
                tempStr = df2.format(temp); // 2位小数
            }

            String noiseStr;
            if (noise == 0) {
                noiseStr = "0";
            }else {
                noiseStr = df1.format(noise); // 1位小数
            }

            String wsStr;
            if (ws == 0) {
                wsStr = "0";
            }else {
                wsStr = df1.format(ws); // 1位小数
            }

            String pressureStr;
            if (pressure == 0) {
                pressureStr = "0";
            }else {
                pressureStr = df3.format(pressure); // 3位小数
            }

            // 拼装数据
            String data = pm25Str+","+pm10Str+","+tempStr+","+humStr+","+noiseStr+","+wsStr+","+wdStr+","+pressureStr;
            String result = "userkey="+snCode+"&data=" +data + "&date="+date;
            returnList.add(result);
        }
        return returnList;
    }
}
